package com.sike.dao.impl;
/**
 * 
 * @company sikejiaoyu
 */
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sike.dao.BaseDao;
import com.sike.exception.DaoException;

@Repository
public class BaseDaoImpl extends SqlMapClientDaoSupport implements BaseDao{
	public static final String INSERT_ERR_MSG = "对数据库执行插入时错误!";
	public static final String UPDATE_ERR_MSG = "对数据库执行修改时错误!";
	public static final String DELETE_ERR_MSG = "对数据库执行删除时错误!";
	public static final String LOAD_ERR_MSG = "对数据库执行查询单条记录时错误!";
	public static final String FIND_ERR_MSG = "对数据库执行查询多条记录时错误!";
	public static final String EXISTS_ERR_MSG = "对数据库执行判断记录是否存在时错误!";
	public static final String COUNT_ERR_MSG = "对数据库执行查询记录数时错误!";
	
	private static Log log = LogFactory.getLog(BaseDaoImpl.class);
	
	public Object insert(String insertSQL, Object param) {
		// TODO Auto-generated method stub
		try {
			return (Object) getSqlMapClientTemplate().insert(insertSQL, param);
		} catch (Exception e) {
			log.error(INSERT_ERR_MSG,e);
			throw new DaoException(INSERT_ERR_MSG, e);
		}
	}

	public int update(String updateSQL, Object param) {
		// TODO Auto-generated method stub
		try {
			return (int)getSqlMapClientTemplate().update(updateSQL, param);
		} catch (Exception e) {
			log.error(UPDATE_ERR_MSG,e);
			throw new DaoException(UPDATE_ERR_MSG, e);
		}
	}

	public int delete(String deleteSQL, Object param) {
		// TODO Auto-generated method stub
		return 0;
	}

	public Object load(String loadSQL, Object param) {
		// TODO Auto-generated method stub
		try {
			return getSqlMapClientTemplate().queryForObject(loadSQL, param);
		} catch (Exception e) {
			log.error(LOAD_ERR_MSG,e);
			throw new DaoException(LOAD_ERR_MSG,e);
		}
	}

	public List find(String key, Object param) {
		// TODO Auto-generated method stub
		try {
			return getSqlMapClientTemplate().queryForList(key, param);
		} catch (Exception e) {
			log.error(FIND_ERR_MSG,e);
			throw new DaoException(FIND_ERR_MSG, e);
		}
	}

	public boolean exists(String key, Object param) {
		// TODO Auto-generated method stub
		return false;
	}

	public long count(String key, Object param) {
		// TODO Auto-generated method stub
		try {
			return ((Long) getSqlMapClientTemplate().queryForObject(key, param));
		} catch (Exception e) {
			log.error(COUNT_ERR_MSG,e);
			throw new DaoException(COUNT_ERR_MSG, COUNT_ERR_MSG);
		}
	}

}
